<?php

class InventoryController extends Zend_Controller_Action {
	
	/**
	 *
	 * @var Cms_Model_Inventory
	 */
	protected $_inventoryModel;
	
	/**
	 *
	 * @var array
	 */
	protected $_forms = array ();
	
	/**
	 *
	 * @var Zend_Controller_Action_Helper_Url
	 */
	protected $_urlHelper;
	
	public function init() {
		$this->_inventoryModel = new Cms_Model_Inventory ();
		
		$this->_urlHelper = $this->_helper->getHelper ( 'url' );
	
	}
	
	public function inventoryAction() {
		
		$action = $this->_getParam ( 'do' );
		
		switch ($action) {
			
			case 'insert' :
				
				$request = $this->getRequest ();
				
				$this->view->backUrl = $this->_urlHelper->url ( array ('do' => 'access' ), 'inventoryInventory', true );
				
				$this->view->inventoryForm = $this->_getInventoryForm ();
				
				if ($request->isPost ()) {
					if (! $this->_forms ['inventoryForm']->isValid ( $request->getPost () )) {
						return $this->render ( 'saveinventory' );
					}
					
					$authService = new Cms_Service_Authentication ();
					$this->_inventoryModel->saveInventory ( $this->_forms ['inventoryForm']->getValues (), $authService->getIdentity ()->fullname );
					$this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->addMessage ( "Success: Purchase has been successfully created" );
					$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'inventoryInventory', true );
				}
				
				$this->_helper->viewRenderer ( 'saveinventory' );
				break;
			
			case 'update' :
				
				$entryId = ( int ) $this->_getParam ( 'id' );
				
				$this->view->backUrl = (null !== $this->_helper->redirectUrl ()) ? $this->_helper->redirectUrl () : $this->_urlHelper->url ( array ('do' => 'access' ), 'inventoryInventory', true );
				
				// if inventory id does not exists - redirect to inventory access
				$inventoryInfo = $this->_inventoryModel->getInventoryByEntryId ( $entryId );
				if ((null === $inventoryInfo)) {
					$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'inventoryInventory', true );
				}
				
				$this->view->inventoryForm = $this->_getInventoryForm ( 'update', $entryId );
				
				$request = $this->getRequest ();
				
				if ($request->isPost ()) {
					
					if (! $this->_forms ['inventoryForm']->isValid ( $request->getPost () )) {
						return $this->render ( 'saveinventory' );
					}
			
					$authService = new Cms_Service_Authentication ();
					$this->_inventoryModel->saveInventory ( $this->_forms ['inventoryForm']->getValues (), $authService->getIdentity ()->fullname, $inventoryInfo );
					$this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->addMessage ( "Success: Purchase has been successfully updated!" );
					
					if (null !== $this->_helper->redirectUrl ()) {
						$this->_redirect ( $this->_helper->redirectUrl (), array ('prependBase' => false ) );
					} else {
						$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'inventoryInventory', true );
					}
				
				}
				
				$inventoryInfo = $inventoryInfo->toArray ();
				
				$mysqlDate = new Zend_Date ( $inventoryInfo ['orderDate'], 'yyyy.MM.dd' );
				$inventoryInfo ['orderDate'] = $mysqlDate->toString ( 'dd/MM/yyyy' );
				
				$this->view->inventoryForm->populate ( $inventoryInfo );
				
				$this->_helper->viewRenderer ( 'saveinventory' );
				
				break;
			
			case 'delete' :
				
				if ($this->getRequest ()->isPost ()) {
					
					$inventorys = $this->_getParam ( 'selected' );
					
					if ((! empty ( $inventorys ) && (is_array ( $inventorys )))) {
						foreach ( $inventorys as $inventory ) {
							// delete inventory
							$this->_inventoryModel->deleteInventory ( $this->_inventoryModel->getInventoryByEntryId ( $inventory ) );
						}
						$this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->addMessage ( "Success: Purchase(s) has been successfully deleted!" );
					}
					
					if (null !== $this->_helper->redirectUrl ()) {
						$this->_redirect ( $this->_helper->redirectUrl (), array ('prependBase' => false ) );
					} else {
						$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'inventoryInventory', true );
					}
				
				} else {
					$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'inventoryInventory', true );
				}
				
				break;
			
			case 'access' :
			default :
				
				if ($this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->hasMessages ()) {
					$this->view->returnMessageSuccess = $this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->getMessages ();
				}
				
				if ($this->_helper->flashMessenger->setNamespace ( 'returnMessageWarning' )->hasMessages ()) {
					$this->view->returnMessageWarning = $this->_helper->flashMessenger->setNamespace ( 'returnMessageWarning' )->getMessages ();
				}
				
				$data = array ();
				$partHeadUrl = '';
				$partPaginationUrl = '';
				
				if ($this->_getParam ( 'sort' )) {
					$data ['sort'] = $this->_getParam ( 'sort' );
					$partPaginationUrl .= '&sort=' . $this->_getParam ( 'sort' );
					
					if ($this->_getParam ( 'order' )) {
						$data ['order'] = $this->_getParam ( 'order' );
						$partPaginationUrl .= '&order=' . $this->_getParam ( 'order' );
					} else {
						$data ['order'] = 'asc';
						$partPaginationUrl .= '&order=asc';
					}
				}
				
				$partHeadUrl .= ($this->_getParam ( 'order' ) == 'asc') ? '&order=desc' : '&order=asc';
				
				if ($this->_getParam ( 'page' )) {
					$data ['page'] = intval ( $this->_getParam ( 'page' ) );
					$partHeadUrl .= '&page=' . intval ( $this->_getParam ( 'page' ) );
				} else { // turn on pagination by default
					$data ['page'] = 1;
				}
				
				if ($this->_getParam ( 'limit' )) {
					$data ['limit'] = ( int ) $this->_getParam ( 'limit' );
					$partHeadUrl .= '&limit=' . $this->_getParam ( 'limit' );
					$partPaginationUrl .= '&limit=' . $this->_getParam ( 'limit' );
				}
				
				if ($this->_getParam ( 'search' )) {
					$data ['search'] = $this->_getParam ( 'search' );
					$partHeadUrl .= '&search=' . $this->_getParam ( 'search' );
					$partPaginationUrl .= '&search=' . $this->_getParam ( 'search' );
				}
				
				$this->view->data = $data;
				$this->view->partHeadUrl = $partHeadUrl;
				$this->view->partPaginationUrl = $partPaginationUrl;
				
				$saleModel = new Cms_Model_Sale ();
				$this->view->saleModel = $saleModel;
				$this->view->inventories = ($this->_inventoryModel->getInventories ( $data )->count () > 0) ? $this->_inventoryModel->getInventories ( $data ) : null;
				$this->_helper->viewRenderer ( 'listinventory' );
				break;
		}
	}
	
	public function creditAction() {
	
		$action = $this->_getParam ( 'do' );
	
		switch ($action) {
				
			case 'insert' :
	
				$request = $this->getRequest ();
	
				$this->view->backUrl = $this->_urlHelper->url ( array ('do' => 'access' ), 'inventoryCredit', true );
	
				$this->view->inventoryForm = $this->_getCreditPurchaseForm();
	
				if ($request->isPost ()) {
					if (! $this->_forms ['inventoryForm']->isValid ( $request->getPost () )) {
						return $this->render ( 'savecredit' );
					}
						
					$authService = new Cms_Service_Authentication ();
					$this->_inventoryModel->saveCreditPurchase ( $this->_forms ['inventoryForm']->getValues (), $authService->getIdentity ()->fullname );
					$this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->addMessage ( "Success: Credit purchase has been successfully created" );
					$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'inventoryCredit', true );
				}
	
				$this->_helper->viewRenderer ( 'savecredit' );
				break;
					
			case 'update' :
	
				$entryId = ( int ) $this->_getParam ( 'id' );
	
				$this->view->backUrl = (null !== $this->_helper->redirectUrl ()) ? $this->_helper->redirectUrl () : $this->_urlHelper->url ( array ('do' => 'access' ), 'inventoryCredit', true );
	
				// if inventory id does not exists - redirect to inventory access
				$inventoryInfo = $this->_inventoryModel->getCreditPurchaseByEntryId ( $entryId );
				if ((null === $inventoryInfo)) {
					$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'inventoryCredit', true );
				}
	
				$this->view->inventoryForm = $this->_getCreditPurchaseForm ( 'update', $entryId );
	
				$request = $this->getRequest ();
	
				if ($request->isPost ()) {
						
					if (! $this->_forms ['inventoryForm']->isValid ( $request->getPost () )) {
						return $this->render ( 'savecredit' );
					}
						
					$authService = new Cms_Service_Authentication ();
					$this->_inventoryModel->saveCreditPurchase ( $this->_forms ['inventoryForm']->getValues (), $authService->getIdentity ()->fullname, $inventoryInfo );
					$this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->addMessage ( "Success: Credit purchase has been successfully updated!" );
						
					if (null !== $this->_helper->redirectUrl ()) {
						$this->_redirect ( $this->_helper->redirectUrl (), array ('prependBase' => false ) );
					} else {
						$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'inventoryCredit', true );
					}
	
				}
	
				$inventoryInfo = $inventoryInfo->toArray ();
	
				$mysqlDate = new Zend_Date ( $inventoryInfo ['orderDate'], 'yyyy.MM.dd' );
				$inventoryInfo ['orderDate'] = $mysqlDate->toString ( 'dd/MM/yyyy' );
	
				$this->view->inventoryForm->populate ( $inventoryInfo );
	
				$this->_helper->viewRenderer ( 'savecredit' );
	
				break;
					
			case 'delete' :
	
				if ($this->getRequest ()->isPost ()) {
						
					$inventorys = $this->_getParam ( 'selected' );
						
					if ((! empty ( $inventorys ) && (is_array ( $inventorys )))) {
						foreach ( $inventorys as $inventory ) {
							// delete inventory
							$this->_inventoryModel->deleteCreditPurchase ( $this->_inventoryModel->getCreditPurchaseByEntryId ( $inventory ) );
						}
						$this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->addMessage ( "Success: Credit purchase(s) has been successfully deleted!" );
					}
						
					if (null !== $this->_helper->redirectUrl ()) {
						$this->_redirect ( $this->_helper->redirectUrl (), array ('prependBase' => false ) );
					} else {
						$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'inventoryCredit', true );
					}
	
				} else {
					$this->_helper->redirector->gotoRoute ( array ('do' => 'access' ), 'inventoryCredit', true );
				}
	
				break;
					
			case 'access' :
			default :
	
				if ($this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->hasMessages ()) {
					$this->view->returnMessageSuccess = $this->_helper->flashMessenger->setNamespace ( 'returnMessageSuccess' )->getMessages ();
				}
	
				if ($this->_helper->flashMessenger->setNamespace ( 'returnMessageWarning' )->hasMessages ()) {
					$this->view->returnMessageWarning = $this->_helper->flashMessenger->setNamespace ( 'returnMessageWarning' )->getMessages ();
				}
	
				$data = array ();
				$partHeadUrl = '';
				$partPaginationUrl = '';
	
				if ($this->_getParam ( 'sort' )) {
					$data ['sort'] = $this->_getParam ( 'sort' );
					$partPaginationUrl .= '&sort=' . $this->_getParam ( 'sort' );
						
					if ($this->_getParam ( 'order' )) {
						$data ['order'] = $this->_getParam ( 'order' );
						$partPaginationUrl .= '&order=' . $this->_getParam ( 'order' );
					} else {
						$data ['order'] = 'asc';
						$partPaginationUrl .= '&order=asc';
					}
				}
	
				$partHeadUrl .= ($this->_getParam ( 'order' ) == 'asc') ? '&order=desc' : '&order=asc';
	
				if ($this->_getParam ( 'page' )) {
					$data ['page'] = intval ( $this->_getParam ( 'page' ) );
					$partHeadUrl .= '&page=' . intval ( $this->_getParam ( 'page' ) );
				} else { // turn on pagination by default
					$data ['page'] = 1;
				}
	
				if ($this->_getParam ( 'limit' )) {
					$data ['limit'] = ( int ) $this->_getParam ( 'limit' );
					$partHeadUrl .= '&limit=' . $this->_getParam ( 'limit' );
					$partPaginationUrl .= '&limit=' . $this->_getParam ( 'limit' );
				}
	
				if ($this->_getParam ( 'search' )) {
					$data ['search'] = $this->_getParam ( 'search' );
					$partHeadUrl .= '&search=' . $this->_getParam ( 'search' );
					$partPaginationUrl .= '&search=' . $this->_getParam ( 'search' );
				}
	
				$this->view->data = $data;
				$this->view->partHeadUrl = $partHeadUrl;
				$this->view->partPaginationUrl = $partPaginationUrl;
	
				$this->view->inventories = ($this->_inventoryModel->getCreditPurchases ( $data )->count () > 0) ? $this->_inventoryModel->getCreditPurchases ( $data ) : null;
				$this->_helper->viewRenderer ( 'listcredit' );
				break;
		}
	}
	
	protected function _getInventoryForm($formState = 'create', $entryId = null) {
		
		$this->_forms ['inventoryForm'] = $this->_inventoryModel->getForm ( 'inventoryInventoryForm' );
		$this->_forms ['inventoryForm']->setMethod ( 'post' );
		
		if ($formState == 'update') {
			$this->_forms ['inventoryForm']->setAction ( $this->_urlHelper->url ( array ('do' => 'update', 'id' => $entryId ), 'inventoryInventory', true ) . $this->_helper->redirectUrl ( true ) );
		} else {
			$this->_forms ['inventoryForm']->setAction ( $this->_urlHelper->url ( array ('do' => 'insert' ), 'inventoryInventory', true ) . $this->_helper->redirectUrl ( true ) );
		}
		
		return $this->_forms ['inventoryForm'];
	}
	
	protected function _getCreditPurchaseForm($formState = 'create', $entryId = null) {
	
		$this->_forms ['inventoryForm'] = $this->_inventoryModel->getForm ( 'inventoryInventoryForm' );
		$this->_forms ['inventoryForm']->setMethod ( 'post' );
	
		if ($formState == 'update') {
			$this->_forms ['inventoryForm']->setAction ( $this->_urlHelper->url ( array ('do' => 'update', 'id' => $entryId ), 'inventoryCredit', true ) . $this->_helper->redirectUrl ( true ) );
		} else {
			$this->_forms ['inventoryForm']->setAction ( $this->_urlHelper->url ( array ('do' => 'insert' ), 'inventoryCredit', true ) . $this->_helper->redirectUrl ( true ) );
		}
	
		return $this->_forms ['inventoryForm'];
	}

}
